// pages/films/index.js
Page({
	regexTest(){ 
		let s = 'a123 www  bbb456 999 ccc789';
		let r, regex = /([a-z]+)(\d+)/g;
		while (r = regex.exec(s)) {
			console.log(r);
		}
	},
	sleep(job,ms) { 
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				resolve(job + ' done!')
			}, ms)
		})
	},
	async Demo() { 
		let r=await this.sleep('job1',5000) 
		console.log(r)
		r=await this.sleep('job2',3000)
		console.log(r)
		r=await this.sleep('job3',1000)
		console.log(r)
	},
	
	async types(e){ 
		let index=e.target.dataset.index 
		if(index==null)return 
		let menuIndex=this.data.menuIndex 
		let menu=this.data.menus[menuIndex] 
		let oldTypeIndex=menu.typeIndex 
		let change={} 
		if(oldTypeIndex!=null)change[`menus[${menuIndex}].types[${oldTypeIndex}].selected`]=false
		change[`menus[${menuIndex}].types[${index}].selected`]=true 
		change[`menus[${menuIndex}].typeIndex`]=index 
		this.setData(change)
		let app=getApp() 
		let type=menu.types[index]
		if(type.list && type.list.length>0) return 
        let s=await app.request({ url:`${this.host}/by/${type.id}.html`})
		let r, list=[], regex = /href="([^"]+)" title="([^"]+)" class="module-item-pic"><img class="lazyload" data-src="([^"]+)"/g;
		while (r = regex.exec(s)) {
			list.push({
				url: r[1], 
				title: r[2], 
				img: r[3],
			})
		}
		console.log(list)
		this.setData({
			[`menus[${menuIndex}].types[${index}].list`]: list,
		})
	},
	menus(e){
		let index=e.target.dataset.index 
		if(index==0){ 
			return
		}
		let oldIndex=this.data.menuIndex 
		let change={ menuIndex:index } 
		change[`menus[${oldIndex}].selected`]=false
		change[`menus[${index}].selected`]=true
		this.setData(change)
	},
	onHide() { 
		console.log("onHide...")
		this.save()
	},
	onUnload() { 
		console.log("onUnload...")
		this.save()
	},
	save(){ 
		let data={ ...this.data } 
		delete data.__webviewId__
		wx.setStorageSync('films', data)
	},
	onLoad(e){
        this.host='https://www.gow64.com'
		let data=wx.getStorageSync('films') 

		if(data)this.setData(data) 
	},
	play(e){
		let index=e.currentTarget.dataset.index 
		wx.navigateTo({ 
			url: `player?index=${index}`,
		})
	},

	data: {
		menuIndex: 2,
		menus: [{
			name: '[刷新]'
		}, {
			name: '[搜索]',
		}, {
			name: '电影',
			selected: true,
			types: [
				{ name: '动作片', id: 14 },
				{ name: '喜剧片', id: 15 },
				{ name: '爱情片', id: 16 },
				{ name: '科幻片', id: 17 },
				{ name: '恐怖片', id: 18 },
				{ name: '剧情片', id: 19 },
				{ name: '战争片', id: 20 },
				{ name: '纪录片', id: 21 },
			]
		}, {
			name: '电视剧',
			types: [
				{ name: '国产剧', id: 5 },
				{ name: '香港剧', id: 6 },
				{ name: '台湾剧', id: 7 },
				{ name: '欧美剧', id: 8 },
				{ name: '韩国剧', id: 9 },
				{ name: '日本剧', id: 10 },
				{ name: '泰国剧', id: 11 },
				{ name: '海外剧', id: 12 },
				{ name: '短剧', id: 13 },
			]
		}, {
			name: '综艺',
			types: [
				{ name: '大陆综艺', id: 22 },
				{ name: '港台综艺', id: 23 },
				{ name: '欧美综艺', id: 24 },
				{ name: '日韩综艺', id: 25 },
			]
		}, {
			name: '动漫',
			types: [
				{ name: '国产动漫', id: 26 },
				{ name: '日韩动漫', id: 27 },
				{ name: '欧美动漫', id: 28 },
				{ name: '海外动漫', id: 29 },
				{ name: '动漫电影', id: 30 },
			]
		}]
	},
	
})